<template>
  <div class="teamwork-apidoc-container">
    <el-row :gutter="15">
      <el-col :span="6">
        <el-card shadow="never">
          <vab-query-form>
            <vab-query-form-left-panel :span="4">
              <el-button :icon="Plus" type="primary" @click="handleEdit">
                添加
              </el-button>
            </vab-query-form-left-panel>
            <vab-query-form-right-panel :span="20">
              <el-form :inline="true" :model="queryForm" @submit.prevent>
                <el-space>
                  <el-form-item>
                    <el-input
                      v-model.trim="queryForm.title"
                      clearable
                      placeholder="请输入标题"
                    />
                  </el-form-item>
                  <el-form-item>
                    <el-button :icon="Search" type="primary" @click="queryData">
                      查询
                    </el-button>
                  </el-form-item>
                </el-space>
              </el-form>
            </vab-query-form-right-panel>
          </vab-query-form>

          <el-table
            v-loading="listLoading"
            :data="list"
            @selection-change="setSelectRows"
          >
            <el-table-column
              align="center"
              show-overflow-tooltip
              type="selection"
              width="40"
            />
            <el-table-column align="left" label="接口名称">
              <template #default="{ row }">
                <el-text style="cursor: pointer" @click="showApidoc(row)">
                  {{ row.ApiTitle }}
                </el-text>
              </template>
            </el-table-column>
            <el-table-column align="center" label="操作" width="100">
              <template #default="{ row }">
                <el-button text @click="handleEdit(row)">编辑</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </el-col>
      <el-col :span="18">
        <el-card shadow="never">
          <div style="padding: 30px; line-height: 20px" v-html="apiMemo"></div>
        </el-card>
      </el-col>
    </el-row>
    <edit ref="editRef" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { getList, doDelete } from '@/api/teamworkApidoc'
  import Edit from './components/TeamworkApidocEdit'
  import { Delete, Plus, Search } from '@element-plus/icons-vue'

  export default defineComponent({
    name: 'TeamworkApidoc',
    components: { Edit },
    setup() {
      const $baseConfirm = inject('$baseConfirm')
      const $baseMessage = inject('$baseMessage')

      const state = reactive({
        editRef: null,
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        apiMemo: '',
        queryForm: {
          pageNo: 1,
          pageSize: 10,
          title: '',
        },
      })

      const setSelectRows = (val) => {
        state.selectRows = val
      }
      const handleEdit = (row) => {
        if (row.ApiID) {
          state.editRef.showEdit(row)
        } else {
          state.editRef.showEdit()
        }
      }
      const showApidoc = (row) => {
        state.apiMemo = row.ApiMemo
      }
      const handleDelete = (row) => {
        if (row.id) {
          $baseConfirm('你确定要删除当前项吗', null, async () => {
            const { msg } = await doDelete({ ids: row.id })
            $baseMessage(msg, 'success', 'vab-hey-message-success')
            await fetchData()
          })
        } else {
          if (state.selectRows.length > 0) {
            const ids = state.selectRows.map((item) => item.id).join()
            $baseConfirm('你确定要删除选中项吗', null, async () => {
              const { msg } = await doDelete({ ids })
              $baseMessage(msg, 'success', 'vab-hey-message-success')
              await fetchData()
            })
          } else {
            $baseMessage('未选中任何行', 'error', 'vab-hey-message-error')
          }
        }
      }
      const handleSizeChange = (val) => {
        state.queryForm.pageSize = val
        fetchData()
      }
      const handleCurrentChange = (val) => {
        state.queryForm.pageNo = val
        fetchData()
      }
      const queryData = () => {
        state.queryForm.pageNo = 1
        fetchData()
      }
      const fetchData = async () => {
        state.listLoading = true
        const {
          data: { list, total },
        } = await getList(state.queryForm)
        state.list = list
        state.total = total
        state.listLoading = false
      }
      onMounted(() => {
        fetchData()
      })

      return {
        ...toRefs(state),
        setSelectRows,
        handleEdit,
        showApidoc,
        handleDelete,
        handleSizeChange,
        handleCurrentChange,
        queryData,
        fetchData,
        Delete,
        Plus,
        Search,
      }
    },
  })
</script>

<style></style>
